<?php
/**
 * Created by PhpStorm.
 * User: yyq
 * Date: 2018/8/22
 * Time: 17:22
 */

namespace Common\Model;


class ProductModel extends CommonModel
{
    const SHOW_YES = 'show';//
    const SHOW_NO  = 'hide';//

    const STATUS_UP     = 'up';//上架
    const STATUS_DOWN   = 'down';//下架

    const HOT_NO    = 'no';//非热门
    const HOT_YES   = 'yes';//热门

    public function getList($where, $limit = '', $order = '') {
        if($_SESSION['cate_id']) {
            $where['cate_id'] = $_SESSION['cate_id'];
        }
        $result = $this->where($where)->limit($limit)->order($order)->select();
        foreach ($result as $k => $v) {
            $result[$k]['cover_photo'] = setUrl($v['cover_photo']);
            $result[$k]['smeta'] = setUrl(json_decode($v['smeta'], true));
        }
        return $result;
    }

    /**
     * @param $order_id
     * @return bool
     * 修改库存和销量
     */
    public function changeStock($order_id) {
        $order_product_model = new OrderProductModel();
        $pro_option_model = new ProOptionModel();
        $products = $order_product_model->where(['order_id' => $order_id])->select();
        foreach ($products as $k => $v) {
            //增加销量
            $this->where(['pro_id' => $v['pro_id']])->save(['sale_num' => ['exp', 'sale_num+1']]);
            //减库存
            $pro_option_model->where(['pro_id' => $v['pro_id'], 'option_name' => $v['option_name']])->save(['stock' => ['exp', 'stock-1']]);
        }
        return true;
    }
}